<template>
  <a-row id="globalHeader" align="center" :wrap="false">
    <a-col flex="auto">
      <div>
        <a-menu
          mode="horizontal"
          :selected-keys="selectedkeys"
          @menu-item-click="doMenuClick"
        >
          <a-menu-item
            key="0"
            :style="{ padding: 0, marginRight: '38px' }"
            disabled
          >
            <div class="title-bar">
              <img class="logo" src="../assets/logo.png" />
              <div class="title">OJ</div>
            </div>
          </a-menu-item>
          <a-menu-item v-for="item in visibleRoute" :key="item.path">
            {{ item.name }}
          </a-menu-item>
        </a-menu>
      </div>
    </a-col>
    <a-col flex="100px">
      <a-button type="primary" shape="round" @click="to">
        <div>
          {{ store.state.user?.loginUser?.userName ?? "未登录" }}
        </div>
      </a-button>
    </a-col>
  </a-row>
</template>

<script setup lang="ts">
import { routes } from "../router/routes";
import { useRoute, useRouter } from "vue-router";
import { computed, ref } from "vue";
import { useStore } from "vuex";
import checkAccess from "@/accesss/checkAccess";
import ACCESS_ENUM from "@/accesss/accessEnum";
import { Message } from "@arco-design/web-vue";

const router = useRouter();
const store = useStore();

//展示的菜单
const visibleRoute = routes.filter((item, index) => {
  if (item.meta?.hideInMenu) {
    return false;
  }
  //根据权限过滤菜单显示
  if (!checkAccess(store.state.user.loginUser, item?.meta?.access as string)) {
    return false;
  }
  return true;
});

const to = () => {
  if (store.state.user.loginUser.userName === "未登录") {
    store.dispatch("user/getLoginUser");
    router.push({
      path: "/user/login",
    });
  } else {
    Message.success("你已登录");
  }
};

//路由跳转后，更新选中的菜单项
router.afterEach((to, from, failure) => {
  selectedkeys.value = [to.path];
});

//默认主页
const selectedkeys = ref(["/"]);

//鼠标事件
const doMenuClick = (key: string) => {
  router.push({
    path: key,
  });
};
</script>

<style scoped>
.title-bar {
  display: flex;
  align-items: center;
}
.title {
  color: #444;
  margin-left: 16px;
}
.logo {
  height: 48px;
}
</style>
